import { EnumUserType } from '../../utils/constant.js'
const app = getApp(),url = app.globalData.url;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    mobile:'',
		code:'',
		FScode:'123123',
		// 倒计时参数
		timeStart:60, //倒计时初始值
    timeCur:null, //当前倒计时显示值
    ifTimeIn:false, //是否倒计时中
		success:false,
		openid:'',

		blurts:false,
		clurts:false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
		var that = this
		that.setData({
			openid:options.openid,
			ask_type:options.type
		})
  },
	onShow(){
		var that = this
		var openid = that.data.openid
		var ask_type = that.data.ask_type
		if(ask_type == 'open_id'){
			wx.showModal({
				title: '提示',
				content: '是否注册过账号',
				cancelText:'否',
				confirmText:'是',
				success (res) {
					if (res.confirm) {
						console.log('用户点击确定')
					} else if (res.cancel) {
						console.log('用户点击取消')
						wx.navigateTo({
							url: '/HomePage/sf/sf?openid='+openid + '&type='+ask_type,
							// url: '/HomePage/sf/sf?openid='+openid + '&type='+ask_type+'&phone_tel='+phone,
						})
					}
				}
			})
		}
		
	},

		

  // 获取手机号
  setMobile(e){
    let that = this
		// console.log(e.detail.value);
		that.setData({
			mobile : e.detail.value.replace(/\s+/g,"")//去空格
		});
  },
  setCode(e){
    let that = this
		that.setData({
      code : e.detail.value.replace(/\s+/g,""),
		});
  },
  // 倒计时
	setTime(){
		let timeCur = this.data.timeCur - 1;
		// console.log(timeCur);
		if(timeCur < 0){
			clearInterval(this.data.timer);
			this.setData({
				ifTimeIn:false
			});
			return false;
		}
		this.setData({
			timeCur : timeCur
		});
  },
  // 获取验证码
	getMobileVerify(){
		var that = this
		if(!that.data.mobile){
			wx.showModal({
				title: '友情提示',
				content: '请输入手机号',
				showCancel: false,
			});
			return false
    }
		if(!/^1\d{10}$/.test(that.data.mobile)){
			wx.showModal({
				title: '友情提示',
				content: '请输入正确的手机号',
				showCancel: false,
			});
			return false;
		}
		wx.showLoading({
		  title: "发送中",
		  mask: true
		});
		//enterprise/sendSmsMessage
		wx.request({
      url:url+'enterprise/sendSmsMessage',
      method: "GET",
      data:{
				mobile_no:that.data.mobile
			},
      success:function(res){
        var ret = res.data
        if(ret.code == 200){
					/* ----请求后台发送验证码成功---- */
					var str=ret.msg;//截取后4位
					var FScode = str.substring(str.length-6);
					// 执行倒计时
					that.setData({
						timeCur : that.data.timeStart,
						timer : setInterval(that.setTime,1000),
						ifTimeIn : true,
						success:true,
						FScode:FScode,
						
						// ifSendMobileVerify : true
					});
					/* ----请求后台发送验证码成功---- */
					// that.setData({listdata:ret.data.content})
					wx.hideLoading();
					wx.showToast({
            title: '发送成功',
            icon:'none'
					})
        }else{
          wx.showToast({
            title: '发送失败',
            icon:'none'
					})
					wx.hideLoading();
        }
      }
    })
		
		
		
		
  },
  // 确定提交
	bindDo(){
		var that = this
		that.setData({ success:false,})
		var phone = that.data.mobile
		var openid = that.data.openid
		var ask_type = that.data.ask_type
		var FScode = that.data.FScode
		if(!that.data.code || FScode != that.data.code){
			wx.showModal({
				title: '友情提示',
				content: '请输入正确验证码',
				showCancel: false,
			});
			that.setData({ success:true,})
			return false;
		}
		wx.request({
			url:url+'wx_commen/up_open_id_by_phone',//up_open_id_by_phone//user_login
			method: "GET",
			data:{
				// ask_type:ask_type,
				open_id:openid,
				phone:phone,
			},
			success:function(resh){
				console.log("是否有电话号码",resh);
				if(resh.data.code == "success"){
					wx.setStorageSync('user_massage', resh.data.massage)
					wx.setStorageSync('phone', resh.data.massage.mobileNum)
          wx.setStorageSync('userType', EnumUserType.GeRen)
					wx.getUserInfo({
						success: res => {
							console.log('用户信息',res.userInfo)
							wx.setStorageSync('userInfo', res.userInfo)
						}
					})
					wx.showToast({
						title: '登录成功',
						icon: 'none',
						duration: 1500
					});
					setTimeout(function(){
						wx.reLaunch({
							url: '/pages/index/index',
						})
					},1500)
					// wx.navigateTo({
					// 	url: '/HomePage/phoneLogin/phoneLogin?openid='+openid,
					// })
				}else{
					// wx.showToast({
					// 	title: '程序错误稍后重试',
					// 	icon:'none',
					// })
					wx.navigateTo({
						url: '/HomePage/sf/sf?openid='+openid + '&type='+ask_type+'&phone_tel='+phone,
					})
				}
			}
		})


	},
	// 手机号验证
	phoneOnBlur(e){
		var that = this
		var blur = e.detail.value
		if(blur == ''){
			console.log('1')
			that.setData({blurts:true})
		}else{
			that.setData({blurts:false})
		}
	},
	// 验证码验证
	codeOnBlur(e){
		var that = this
		var clur = e.detail.value
		if(clur == ''){
			console.log('1')
			that.setData({clurts:true})
		}else{
			that.setData({clurts:false})
		}
	},

})
